package com.itgaoshu.controller;

import com.alibaba.druid.sql.visitor.functions.If;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.bean.Doctor;
import com.itgaoshu.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医生列表
 */
@RestController
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService ;
    //查询所有医生相对应的信息
    @RequestMapping("selectAllDoctor")
    public Object selectAllDoctor(@RequestParam(required = true,defaultValue = "1") int pageNum){
        PageHelper.startPage(pageNum,5);//每页5条数据
        List<Doctor> allDoctors = doctorService.selectAllDoctor();
        PageInfo pageInfo = new PageInfo(allDoctors);
        Map<String, Object> allDoctor = new HashMap<>();
        //这是layui要求返回的json数据格式
        allDoctor.put("code", 0);
        allDoctor.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        allDoctor.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        allDoctor.put("data", pageInfo.getList());

        return allDoctor;
    }
    /*
     * 添加医生
     * */
    @RequestMapping("addDoctor")
    public Object addDoctor(Doctor doctor){
        int result = doctorService.addDoctor(doctor);
        if(result==0){
            int add = doctorService.addDoctor(doctor);
            if(add==1){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else{
            return doctor.getDoctorName()+"已存在";
        }
    };
    //模糊查询医生相对应的信息
    @RequestMapping("selectDoctor")
    //@ResponseBody//返回json格式
    public Object selectDoctor(Doctor doctor ,@RequestParam(required = true,defaultValue = "1") int pageNum){
        //通过PageHelper调用startPage方法设置查询的页数和每页查询的记录条数
        PageHelper.startPage(pageNum,5);//每页5条数据
        List<Doctor> allDepartments =doctorService.selectDoctor(doctor);
        //将查询的结果以及分页所需的数据封装到pageInfo对象中
        PageInfo pageInfo=new PageInfo(allDepartments);
        System.out.print(pageInfo);
        Map<String,Object> allDepartment=new HashMap<>();
        //这是LayUI要求返回的json数据形式
        allDepartment.put("code",0);
        allDepartment.put("msg","");
        //将全部数据的条数作为count传给前台（一共有多少条数据）
        allDepartment.put("count",pageInfo.getTotal());
        //将分页后的数据返回（每一页显示的数据）
        allDepartment.put("data",pageInfo.getList());
        return allDepartment;
    };
   //根据类别回显医生的方法
    @RequestMapping("selectDoctorbydepartmentid+")
    @ResponseBody
    public String  selectDoctorbydepartmentid (int departmentid ){
      List<Doctor>  doctors= doctorService.selectDoctorBYdepartmentid(departmentid);
        if (doctors!=null){//显示成功
            return "";

        }  else {//显示失败
            return "";

        }

    }

}
